package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.topic;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.TopicResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

@ActivityScope
public class TopicPresenter extends BasePresenter<DynamicContract.Model, DynamicContract.TopicView> {

    public static final int ADD_TOPIC = 1;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public TopicPresenter(DynamicContract.Model model, DynamicContract.TopicView view) {
        super(model, view);
    }

    public void fetchTopic(String keyword) {
        mModel.fetchTopic(keyword)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<TopicResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<TopicResult> result) {
                        mRootView.onFetchTopic(result.getList());
                    }
                });
    }

    public void getTopicById(int id) {
        mModel.getTopicById(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<TopicResult>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull TopicResult result) {
                        mRootView.onGetTopic(result);
                    }
                });
    }

    public void addTopic(String topicTitle) {
        mModel.addTopic(topicTitle)
                .doOnSubscribe(act -> mRootView.showLoading("正在操作"))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ADD_TOPIC) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.onTopicAdd(result);
                    }
                });
    }

    public void joinGroup(String groupId) {
        mModel.joinGroup(groupId)
                .doOnSubscribe(act -> mRootView.showLoading("正在加入..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<String>() {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        mRootView.onTopicGroupJoin(result);
                    }
                });
    }
}
